{include file="header.html"}
<style>
	.list td {
		line-height: 34px;
		font-size: 10px;
		color: #666;
		text-align: center;
	}
	table.dataTable { width: 500px;}
	.import-box, .goods-list {
		overflow: auto;
		width: 100%;
	}
	.fixed-tools {
		position: fixed;
		bottom: 0;
		width: 100%;
		padding: 10px;
		-webkit-box-sizing: border-box;
		-moz-box-sizing: border-box;
		box-sizing: border-box;
		background: #FFF;
		border-top: 1px solid #d7d7d7;
	}
	.fl { float: left;}
	.fr { float: right; }
</style>
<title>出库申请</title>
<div id="rightTop">
	<p>出库申请</p>
</div>
<div class="mrightTop">
	<div class="fontl">
		<input type="hidden" name="app" value="getoutstore"/>
		<input id='act' type="hidden" name="act" value="index"/>

		<div class="c1">
			<div class="c2">选择仓库</div>
			<div class="c3">
				<select class="choose-store form-control">
					<option value="">-- 无可选仓库 --</option>
				</select>
			</div>
		</div>
		<div class="c1">
			<input type="button"  class="formbtn import-goods" style="margin-left: 5px"  value="引入货物"/>
			<input type="button"  class="formbtn order-save" style="margin-left: 5px"  value="创建"/>
		</div>
		<div class="c4"></div>
	</div>
	<div class="clear:both;"></div>
</div>
<div class="import-box" style="display: none;">
	<table cellspacing="0" class="dataTable" style="margin-top: 2px">
		<tr class="tatr1">
			<td width="30"></td>
			<td width="100">入库单号</td>
			<td >物料名称</td>
			<td width="80">商品编码</td>
			<td width="100">物料规格</td>
			<td width="70">件数</td>
		</tr>
		<tbody class="list">

		</tbody>
		<tbody class="tmp" style="display:none;">
		<tr>
			<td><input type="checkbox" class="SS_Tid" value=""></td>
			<td class="SIM_Code"></td>
			<td class="sm_name"></td>
			<td class="sm_ciqcode"></td>
			<td class="SM_Describe"></td>
			<td class="firstcount"></td>
		</tr>
		</tbody>
	</table>
	<div></div>
	<div class="fixed-tools">
		<div class="fl"></div>
		<input type="button"  class="formbtn import-goods-save" style="margin-left: 5px"  value="确定"/>
		<div class="fr">
			<input type="text" class="search-input queryInput2" placeholder="搜索货物">
			<input type="button"  class="formbtn list-filter" style="margin-left: 5px"  value="搜索"/>
		</div>
	</div>
	<div style="margin-top: 20px">
		&nbsp;
	</div>
</div>
<div class="goods-list">
	<table cellspacing="0" class="dataTable" style="margin-top: 2px">
		<tr class="tatr1">
			<td width="30"></td>
			<td width="100">入库单号</td>
			<td >物料名称</td>
			<td width="80">商品编码</td>
			<td width="100">物料规格</td>
			<td width="70">件数</td>
		</tr>
		<tbody class="list">

		</tbody>
		<tbody class="tmp" style="display:none;">
		<tr>
			<td><input type="checkbox" class="SS_Tid" value=""></td>
			<td class="SIM_Code"></td>
			<td class="sm_name"></td>
			<td class="sm_ciqcode"></td>
			<td class="SM_Describe"></td>
			<td><input class="form-control firstcount" value="" style="width: 60px; font-size: 10px;" ></td>
		</tr>
		</tbody>
	</table>
</div>

<script>
$(function () {
    // 货物搜索
    function goods_filter() {
        var keyword = $('.import-box .search-input').val().replace(/(^\s+|\s+$)/, '');
        var reg = new RegExp(keyword, 'img');
        $('.import-box .list tr').each(function () {
            if(keyword == '' || reg.test($(this).html())) {
                $(this).show();
			} else {
                $(this).hide();
			}
		});
	}

    $('.import-box .search-input').change(function () {
        goods_filter();
    });
    $('.import-box .search-input').keyup(function () {
        goods_filter();
    });
	$('.import-box .list-filter').click(function () {
        goods_filter();
	});


	// 获取仓库列表
	function initStore() {
		$.get('index.php?app=outstore&act=GetStoreList', function (result) {
		    if(typeof result !== 'object') {
		        result = eval('('+result+')');
			}
		    console.log(result);

		    if(result.done == 1) {
		        console.log(1111);
		        var datas = result.retval.result;
				var html = '';
				for(var key in datas) {
					html += '<option value="'+datas[key].BOI_Sid+'">'+datas[key].BOI_SName+'</option>';
				}
				$('.choose-store').html(html).change();
			} else {
		        if(result.msg == '该用户未登录') {
                    alert('请先登录');
					location.href = '/index.php?app=login';
				} else {
                    alert(result.msg);
				}
			}
		}, 'json');
    }

    var tid = 0;
	var storeID = 0;
	var goodsList = null;

    // 保存出库表头
	$('.order-save').click(function () {
	    if(!tid) {
	        // 创建订单
			createOrder();
			return;
		}

		// 提交订单
		var data = [];
	    $('.goods-list .SS_Tid:checked').each(function () {
	        var id = $(this).val();
	        var num = $(this).parents('tr').find('.firstcount').val();

	        num = parseFloat(num);
	        num = isNaN(num) ? 0 : num;
	        if(num == 0) return;
	        data.push(id + '|' + num);
		});

	    if(data.length == 0) {
	        alert('请选择货物');
			return;
		}

	    var goodsListInfo = data.join(',');
	    // 保存物品
        $.post('index.php?app=outstore&act=AddOutBillList', {
            tid: tid,
            goodsListInfo: goodsListInfo
        }, function (result) {
            if(typeof result !== 'object') {
                result = eval('('+result+')');
            }
            console.log(result);
            if(result.done == 1) {
                alert('提交成功');
            } else {
                alert(result.msg);
            }
        }, 'json');
	});

	// 引入货物
	$('.import-goods').click(function () {
	    if(goodsList) {
	        showImportGoods();
	        return;
		}
        $.post('index.php?app=outstore&act=GetStoreGoodsList', {
            tid: tid
        }, function (result) {
            if(typeof result !== 'object') {
                result = eval('('+result+')');
            }
            console.log(result);
            if(result.done == 1) {
                goodsList = [];
                var goods = result.retval.result;
                for(var key in goods) {
                    goodsList[goods[key].SS_Tid] = goods[key];
				}
				initImportTab();
                showImportGoods();
            } else {
                alert(result.msg);
            }
        }, 'json');
	});

	// 引入货物保存
	$('.import-goods-save').click(function () {
	    var checkedList = [];
	    $('.import-box .SS_Tid:checked').each(function () {
	        var id = $(this).val();
            checkedList.push(goodsList[id]);
		});

	    if(checkedList.length === 0) {
	        alert('请选择货物');
	        return;
		}

	    // 清空历史货物
	    $('.goods-list .list').html('');
	    var tmp = $('.goods-list .tmp tr');
        for(var key in checkedList) {
            var row = checkedList[key];
            var rowobj = tmp.clone();
            for(var k in row) {
                if(k == 'SS_Tid' || k == 'firstcount') {
                    rowobj.find('.'+k).val(row[k]);
                } else {
                    rowobj.find('.'+k).text(row[k]);
                }
            }
            rowobj.appendTo($('.goods-list .list'));
        }
        hideImportGoods();
	});

	// 显示导入列表
    function showImportGoods() {
        $('.import-box').show().siblings('div').hide();
    }
    function hideImportGoods() {
        $('.import-box').hide().siblings('div').show();
    }

	// 初始化 导入列表
	function initImportTab() {
		var tmp = $('.import-box .tmp tr');
		for(var key in goodsList) {
		    var row = goodsList[key];
		    var rowobj = tmp.clone();
		    for(var k in row) {
		        if(k == 'SS_Tid') {
		            rowobj.find('.'+k).val(row[k]);
				} else {
                    rowobj.find('.'+k).text(row[k]);
				}
			}
			rowobj.appendTo($('.import-box .list'));
		}
	}

	// 创建订单
	function createOrder() {
        storeID = $('.choose-store').val();
        $.post('index.php?app=outstore&act=AddOutBillHead', {
            storeID: storeID
		}, function (result) {
            if(typeof result !== 'object') {
                result = eval('('+result+')');
            }
            console.log(result);
            if(result.done == 1) {
                tid = result.retval.result;
                $('.order-save').val('保存');
            } else {
                alert(result.msg);
            }
        }, 'json');
	}

    initStore();
})
</script>